﻿@using CarCareTracker.Helper
@inject IConfigHelper config
@inject ITranslationHelper translator
@model GasRecordInputContainer
@{
    var userConfig = config.GetUserConfig(User);
    var useMPG = userConfig.UseMPG;
    var useUKMPG = userConfig.UseUKMPG;
    var userLanguage = userConfig.UserLanguage;
    var useThreeDecimals = userConfig.UseThreeDecimalGasCost;
    var useThreeDecimalsConsumption = userConfig.UseThreeDecimalGasConsumption;
    var useKwh = Model.UseKwh;
    var useHours = Model.UseHours;
    var isNew = Model.GasRecord.Id == 0;
    var useUnitFuelCost = userConfig.UseUnitForFuelCost;
    string consumptionUnit;
    string distanceUnit;
    if (useKwh)
    {
        consumptionUnit = "kWh";
    } else if (useUKMPG)
    {
        consumptionUnit = @translator.Translate(userLanguage, "liters");
    }
    else
    {
        consumptionUnit = useMPG ? @translator.Translate(userLanguage, "gallons") : @translator.Translate(userLanguage, "liters");
    }
    if (useHours)
    {
        distanceUnit = @translator.Translate(userLanguage, "hours");
    }
    else if (useUKMPG)
    {
        distanceUnit = @translator.Translate(userLanguage, "miles");
    }
    else
    {
        distanceUnit = useMPG ? @translator.Translate(userLanguage, "miles") : @translator.Translate(userLanguage, "kilometers");
    }
}
<div class="modal-header">
    <h5 class="modal-title">@(isNew ? translator.Translate(userLanguage, "Add New Gas Record") : translator.Translate(userLanguage, "Edit Gas Record"))<small style="display:none; @(isNew ? "" : "cursor:pointer;")" class="cached-banner ms-2 text-warning" onclick='@(isNew ? "" : $"showEditGasRecordModal({Model.GasRecord.Id}, true)" )'>@translator.Translate(userLanguage, "Unsaved Changes")</small></h5>
    <button type="button" class="btn-close" onclick="hideAddGasRecordModal()" aria-label="Close"></button>
</div>
<div class="modal-body" onkeydown="handleEnter(this)">
    <form>
        <div class="form-group">
            <div class="row">
                <div class="col-md-6 col-12">
                    <input type="text" id="workAroundInput" style="height:0px; width:0px; display:none;">
                    <label for="gasRecordDate">@translator.Translate(userLanguage,"Date")</label>
                    <div class="input-group">
                        <input type="text" id="gasRecordDate" placeholder="@translator.Translate(userLanguage,"Date refueled")" class="form-control" value="@Model.GasRecord.Date">
                        <span class="input-group-text"><i class="bi bi-calendar-event"></i></span>
                    </div>
                    <label for="gasRecordMileage">@($"{translator.Translate(userLanguage,"Odometer Reading")}({distanceUnit})")</label>
                    <div class="input-group">
                        <input type="number" inputmode="numeric" id="gasRecordMileage" class="form-control" placeholder="@translator.Translate(userLanguage,"Odometer reading when refueled")" value="@(isNew || Model.GasRecord.Mileage == default ? "" : Model.GasRecord.Mileage)">
                        @if (isNew)
                        {
                            <div class="input-group-text">
                                <button type="button" class="btn btn-sm btn-primary zero-y-padding" onclick="getLastOdometerReadingAndIncrement('gasRecordMileage')"><i class="bi bi-plus"></i></button>
                            </div>
                        }
                    </div>
                    <label for="gasRecordGallons">@($"{translator.Translate(userLanguage, "Fuel Consumption")}({consumptionUnit})")</label>
                    <input type="text" inputmode="decimal" onkeydown="interceptDecimalKeys(event)" onkeyup="@(useThreeDecimalsConsumption ? "fixDecimalInput(this, 3)" : "fixDecimalInput(this, 2)")" id="gasRecordGallons" class="form-control" placeholder="@translator.Translate(userLanguage,"Amount of gas refueled")" value="@(isNew ? "" : Model.GasRecord.Gallons)">
                    <div class="form-check form-switch">
                        <input class="form-check-input" type="checkbox" role="switch" id="gasIsFillToFull" checked="@Model.GasRecord.IsFillToFull">
                        <label class="form-check-label" for="gasIsFillToFull">@translator.Translate(userLanguage,"Is Filled To Full")</label>
                    </div>
                    <div class="form-check form-switch">
                        <input class="form-check-input" type="checkbox" role="switch" id="gasIsMissed" checked="@Model.GasRecord.MissedFuelUp">
                        <label class="form-check-label" for="gasIsMissed">@translator.Translate(userLanguage,"Missed Fuel Up(Skip MPG Calculation)")</label>
                    </div>
                    <label for="GasRecordCost">@translator.Translate(userLanguage,"Cost")</label>
                    @if (isNew)
                    {
                        <div class="input-group">
                            <input type="text" onkeydown="interceptDecimalKeys(event)" onkeyup="@(useThreeDecimals ? "fixDecimalInput(this, 3)" : "fixDecimalInput(this, 2)")" inputmode="decimal" id="gasRecordCost" class="form-control" placeholder="@translator.Translate(userLanguage,"Cost of gas refueled")" value="@(isNew ? "" : Model.GasRecord.Cost)">
                            <div class="input-group-text">
                                <select class="form-select form-select-sm" id="gasCostType">
                                    <!option @(useUnitFuelCost ? "" : "selected") value="total">@translator.Translate(userLanguage,"Total")</!option>
                                    <!option @(useUnitFuelCost ? "selected" : "") value="unit">@translator.Translate(userLanguage,"Unit")</!option>
                                </select>
                            </div>
                        </div>
                    } else
                    {
                        <input type="text" inputmode="decimal" onkeydown="interceptDecimalKeys(event)" onkeyup="@(useThreeDecimals ? "fixDecimalInput(this, 3)" : "fixDecimalInput(this, 2)")" id="gasRecordCost" class="form-control" placeholder="@translator.Translate(userLanguage,"Cost of gas refueled")" value="@(isNew ? "" : Model.GasRecord.Cost)">
                    }
                    <label for="gasRecordTag">@translator.Translate(userLanguage,"Tags(optional)")</label>
                    <select multiple class="form-select" id="gasRecordTag">
                        @foreach (string tag in Model.GasRecord.Tags)
                        {
                            <!option value="@tag">@tag</!option>
                        }
                    </select>
                    @await Html.PartialAsync("_ExtraField", Model.GasRecord.ExtraFields)
                </div>
                <div class="col-md-6 col-12">
                    <label for="gasRecordNotes">@translator.Translate(userLanguage,"Notes(optional)")<a class="link-underline link-underline-opacity-0" onclick="showLinks(this)"><i class="bi bi-markdown ms-2"></i></a></label>
                    <textarea id="gasRecordNotes" class="form-control" rows="5">@Model.GasRecord.Notes</textarea>
                    <div>
                        @await Html.PartialAsync("_UploadedFiles", Model.GasRecord.Files)
                        @await Html.PartialAsync("_FileUploader", Model.GasRecord.Files.Any())
                    </div>
                    <div id="filesPendingUpload"></div>
                </div>
            </div>
        </div>
    </form>
</div>
<div class="modal-footer">
    @if (!isNew)
    {
        <button type="button" class="btn btn-danger" onclick="deleteGasRecord(@Model.GasRecord.Id)" style="margin-right:auto;">@translator.Translate(userLanguage,"Delete")</button>
    }
    <button type="button" class="btn btn-secondary" onclick="hideAddGasRecordModal()">@translator.Translate(userLanguage,"Cancel")</button>
    @if (isNew)
    {
        <button type="button" class="btn btn-primary" onclick="saveGasRecordToVehicle()">@translator.Translate(userLanguage,"Add New Gas Record")</button>
    }
    else if (!isNew)
    {
        <button type="button" class="btn btn-primary" onclick="saveGasRecordToVehicle(true)">@translator.Translate(userLanguage,"Edit Gas Record")</button>
    }
</div>
<script>
    function getGasRecordModelData(){
        return { id: @Model.GasRecord.Id}
    }
</script>